require "#{RAILS_ROOT}/app/classes/cat/rules/rule"
require "#{RAILS_ROOT}/app/classes/cat/cat_item"

#* starting_rule.rb
#* Copyright (C) 2010  Embian Inc.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or 
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with This program.  If not, see <http://www.gnu.org/licenses/>.
#  
#* Defines "Item Selection Rule" for CAT
#* Author: KyoungMo Yang(E-mail : mo@embian.com)
#* Modified: 2010-11-30
#* References: Rule, UserProfile, CATItem, Item
module StartingRule
  include Rule
  
  #* Functionailities
  # * Implements Rule#run
  #* Parameters 
  # * [*args] : argement list
  #* Return
  # * N/A
  #* Note
  # * Override Rule#run
  def run(*args)
    return middle_diff
  end
  
  private
  #* Functionailities
  # * Implements "Middle Difficulty Rule" as Starting Rule of CAT
  #* Parameters 
  # * N/A
  #* Return
  # * [irt_item] : Selected an instance of IRTItem
  #* Note
  # * N/A
  def middle_diff
    #middle difficulty item
    exams = Exam.find(:all, :conditions=>{:user_id=>profile.user_id, :skill_id=>profile.classfication})
    previous_items = []
    exams.each do |exam|
      exam.exam_parts.each do |part|
        previous_items << part.item
      end
    end
    exclusion_condition = _get_exclusion_query1(previous_items)
    
    items = Item.connection.select_all("SELECT id, answer, irt_a, irt_b, irt_c from items where skill_id=#{profile.classfication}
                                                                                         and irt_b between -1.0 and 1.0
                                                                                         and irt_a is not null and irt_a!=0
                                                                                         and irt_b is not null and irt_b!=0
                                                                                         #{exclusion_condition} ")
    
    #TODO : difficulty를 조정하여 하나의 문항을 선택할 수 있도록 ...
    raise "Not found item" if items.empty?
      
    if items.size > 1
      #TODO : item overheating problems
      item = items.first
    else
      item = items.first
    end
    
    cat_item = CATItem.new(item['id'], item['irt_a'].to_f, item['irt_b'].to_f, item['irt_c'].to_f)
    cat_item.answer = item['answer']
    return cat_item
  end
  
  
  def _get_exclusion_query1(previous_items)
    return '' if previous_items.empty?
    query = []
    previous_items.each do |item|
      query << item.id
    end
    query = query.join(' !=id and ')
    return "and #{query} !=id " unless query.to_s.strip.empty?
  end
end